<template>
  <view class="news-detail-page">
    <!-- 返回按钮 -->
    <!-- <view class="nav-back" @click="navigateBack">
      <view class="cuIcon-back"></view>
    </view> -->

    <!-- 新闻内容 -->
    <scroll-view scroll-y class="content-container">
      <!-- 新闻大图 -->
      <image
        :src="_imgUrl + newsData.photo"
        mode="aspectFill"
        class="news-banner"
      />

      <!-- 主体内容 -->
      <view class="main-content">
        <!-- 标题区 -->
        <view class="title-section">
          <text class="title">{{ newsData.title }}</text>
          <view class="meta">
            <text class="time">{{ newsData.createTime }}</text>
            <view class="views">
              <view class="cuIcon-attention"></view>
              <!-- {{}} -->
            </view>
          </view>
        </view>

        <!-- 内容区 -->
        <view class="content-section">
          <rich-text :nodes="newsData.content"></rich-text>
        </view>

        <!-- 评论区 -->
        <view class="comment-section">
          <view class="section-header">
            <text class="title">热门评论</text>
            <view class="decor-line"></view>
          </view>

          <!-- 评论列表 -->
          <view
            v-for="(comment, index) in commentList"
            :key="index"
            class="comment-card"
          >
            <image
              :src="_imgUrl + comment.userPhoto"
              class="user-avatar"
              mode="aspectFill"
            />
            <view class="comment-content">
              <view class="user-info">
                <text class="username">{{ comment.userName }}</text>
                <text class="time">{{ comment.createTime }}</text>
              </view>
              <text class="comment-text">{{ comment.plContent }}</text>
            </view>
          </view>

          <!-- 评论输入 -->
          <view class="comment-input">
            <input
              v-model="newComment"
              placeholder="写下你的评论..."
              class="input-box"
            />
            <view class="submit-btn" @click="submitComment">
              <text>发送</text>
              <view class="cuIcon-right"></view>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      newsData: {
        // title: "数字农业助力乡村振兴新突破",
        // image: "/static/news/detail.jpg",
        // publishTime: "2024-02-20 09:30",
        // views: 2356,
        // content: `<p>农业农村部最新数据显示，通过物联网、大数据等新一代信息技术的应用...</p>
        //         <img src="/static/news/content1.jpg" style="width:100%;margin:20rpx 0">
        //         <p>在浙江某智慧农业示范基地，技术人员演示了智能温室控制系统...</p>`,
      },
      commentList: [
        // {
        //   avatar: "/static/avatar/1.jpg",
        //   username: "农业先锋",
        //   time: "2小时前",
        //   content: "科技赋能农业，未来可期！",
        // },
        // {
        //   avatar: "/static/avatar/2.jpg",
        //   username: "田间观察员",
        //   time: "3小时前",
        //   content: "希望这些技术能尽快普及到我们这里",
        // },
      ],
      newComment: "",
      _imgUrl: "",
      newsId: "",
    };
  },
  onLoad(options) {
    this._imgUrl = this.$base.url;
    this.newsId = options.id;
    Promise.all([this.getnewsData(), this.getcommentList()]);
    // this.getnewsData();
  },
  methods: {
    async getcommentList() {
      try {
        let res = await this.$api.list("comment", {
          tzId: this.newsId,
          plType: "咨询",
        });
        if (res.code == 200) {
          this.commentList = res.rows;
        }
      } catch (error) {}
    },
    async getnewsData() {
      try {
        let res = await this.$api.info("information", this.newsId);
        if (res.code == 200) {
          this.newsData = res.data;
        }
      } catch (error) {}
    },
    navigateBack() {
      uni.navigateBack();
    },
    async submitComment() {
      if (this.newComment.trim()) {
        try {
          let res = await this.$api.add("comment", {
            tzId: this.newsId,
            plType: "咨询",
            plContent: this.newComment,
            userId: uni.getStorageSync("userInfo").userId,
            userName: uni.getStorageSync("userInfo").userName,
            userPhoto: uni.getStorageSync("userInfo").avatar,
          });
          if (res.code == 200) {
            this.getcommentList();
            this.newComment = "";
            uni.pageScrollTo({ scrollTop: 9999, duration: 300 });
            // this.commentList.unshift({
            //   avatar: "/static/avatar/default.jpg",
            //   username: "新用户",
            //   time: "3小时前",
            //   content: this.newComment,
            // })
          }
        } catch (error) {}
        // this.commentList.unshift({
        //   avatar: "/static/avatar/default.jpg",
        //   username: "新用户",
        //   time: "刚刚",
        //   content: this.newComment,
        // });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.news-detail-page {
  background: linear-gradient(15deg, #f8f9ff 0%, #f1f3fe 100%);
  min-height: 100vh;
}

.nav-back {
  position: fixed;
  top: 60rpx;
  left: 32rpx;
  z-index: 10;
  .cuIcon-back {
    font-size: 48rpx;
    color: #2d3748;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 50%;
    padding: 16rpx;
    box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.1);
  }
}

.content-container {
  height: 100vh;
  padding-bottom: 220rpx;
}

.news-banner {
  width: 100%;
  height: 480rpx;
}

.main-content {
  padding: 0 32rpx;
}

.title-section {
  margin: 48rpx 0;
  .title {
    font-size: 40rpx;
    font-weight: 700;
    color: #2d3748;
    line-height: 1.4;
  }
  .meta {
    display: flex;
    align-items: center;
    margin-top: 32rpx;
    color: #718096;
    font-size: 24rpx;
    .views {
      margin-left: 40rpx;
      display: flex;
      align-items: center;
      .cuIcon-attention {
        margin-right: 8rpx;
      }
    }
  }
}

.content-section {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  border-radius: 24rpx;
  padding: 32rpx;
  box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.1);
  line-height: 1.8;
  font-size: 30rpx;
  color: #4a5568;
}

.comment-section {
  margin-top: 48rpx;
  .section-header {
    margin-bottom: 40rpx;
    .title {
      font-size: 36rpx;
      font-weight: 600;
      color: #2d3748;
    }
    .decor-line {
      height: 4rpx;
      width: 80rpx;
      background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
      margin-top: 16rpx;
    }
  }
}

.comment-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  border-radius: 24rpx;
  padding: 32rpx;
  margin-bottom: 32rpx;
  display: flex;
  box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.1);

  .user-avatar {
    width: 80rpx;
    height: 80rpx;
    border-radius: 50%;
    margin-right: 24rpx;
    flex-shrink: 0;
  }

  .comment-content {
    flex: 1;
    .user-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16rpx;
      .username {
        font-size: 28rpx;
        font-weight: 500;
        color: #2d3748;
      }
      .time {
        font-size: 24rpx;
        color: #a0aec0;
      }
    }
    .comment-text {
      font-size: 28rpx;
      color: #4a5568;
      line-height: 1.6;
    }
  }
}

.comment-input {
  position: fixed;
  bottom: 20rpx;
  display: flex;
  width: 92%;
  align-items: center;
  margin-top: 48rpx;
  padding: 24rpx;
  box-sizing: border-box;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 48rpx;
  box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.1);

  .input-box {
    flex: 1;
    padding: 0 32rpx;
    font-size: 28rpx;
    color: #2d3748;
  }

  .submit-btn {
    background: linear-gradient(135deg, #667eea, #764ba2);
    border-radius: 40rpx;
    padding: 16rpx 32rpx;
    color: white;
    display: flex;
    align-items: center;
    font-size: 28rpx;
    .cuIcon-right {
      margin-left: 12rpx;
      font-size: 32rpx;
    }
  }
}
</style>
